package com.angel.infrastructure.db.repository;

import com.angel.domain.angeluser.aggregates.AngelUser;
import com.angel.domain.angeluser.entities.AngelLevel;
import com.angel.domain.angeluser.entities.AngelUserInfo;
import com.angel.domain.angeluser.factory.AngelUserFactory;
import com.angel.domain.angeluser.repositories.AngelUserRepository;
import com.angel.domain.angeluser.valueobjects.*;
import com.angel.infrastructure.db.mapper.AngelLevelMapper;
import com.angel.infrastructure.db.mapper.AngelUserInfoMapper;
import com.angel.infrastructure.db.mapper.AngelUserLevelMapper;
import com.angel.infrastructure.db.mapper.AngelUserWeChatInfoMapper;
import com.angel.infrastructure.db.po.AngelLevelPO;
import com.angel.infrastructure.db.po.UserInfoPO;
import com.angel.infrastructure.db.po.UserLevelPO;
import com.angel.infrastructure.db.po.UserWechatInfoPO;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Repository;

import java.util.Optional;


/*
 * 更新关注微信公众号： 为了offer
 * @author parkin
 * @description
 * @date 2021/11/13
 */
@Repository
public class DefaultAngelUserRepository implements AngelUserRepository {

    private final AngelUserInfoMapper angelUserInfoMapper;
    private final AngelUserWeChatInfoMapper angelUserWeChatInfoMapper;
    private final AngelUserLevelMapper angelUserLevelMapper;
    private AngelLevelMapper angelLevelMapper;

    public DefaultAngelUserRepository(AngelUserInfoMapper angelUserInfoMapper,
                                      AngelUserWeChatInfoMapper angelUserWeChatInfoMapper,
                                      AngelUserLevelMapper angelUserLevelMapper,
                                      AngelLevelMapper angelLevelMapper) {
        this.angelUserInfoMapper = angelUserInfoMapper;
        this.angelUserWeChatInfoMapper = angelUserWeChatInfoMapper;
        this.angelUserLevelMapper = angelUserLevelMapper;
        this.angelLevelMapper = angelLevelMapper;
    }

    @Override
    public AngelUser save(AngelUser angelUser) {
        UserInfoPO userInfoPO = new UserInfoPO(
                angelUser.getAngelUserId().getId(),
                angelUser.getAngelUserInfo().getUserBasicInfo().getNickName(),
                angelUser.getAngelUserInfo().getUserBasicInfo().getHeadImage().getHeadImage()
        );

        angelUserInfoMapper.insert(userInfoPO);

        UserWechatInfoPO wechatInfoPO = new UserWechatInfoPO(
                angelUser.getAngelUserId().getId(),
                angelUser.getAngelUserInfo().getWeChatInfo().getNickName(),
                angelUser.getAngelUserInfo().getWeChatInfo().getAvatarUrl(),
                angelUser.getAngelUserInfo().getWeChatInfo().getOpenId()
        );
        angelUserWeChatInfoMapper.insert(wechatInfoPO);

        UserLevelPO userLevelPO = new UserLevelPO(
                angelUser.getAngelUserId().getId(),
                angelUser.getAngelUserLevel().getNowUserLevel().getAngelLevelId(),
                angelUser.getAngelUserLevel().getNowExperienceValue()
        );

        angelUserLevelMapper.insert(userLevelPO);

        return angelUser;
    }

    @Override
    public Optional<AngelUser> get(AngelUserId angelUserId) {
        // mybatis3.5 已经支持返回optional对象 mybatis-plus最新版依赖得是3.4
        UserInfoPO userInfoPO = angelUserInfoMapper.selectById(angelUserId.getId());
        if (userInfoPO == null) {
            return Optional.empty();
        }

        UserWechatInfoPO wechatInfoPO = angelUserWeChatInfoMapper.selectOne(
                Wrappers.<UserWechatInfoPO>lambdaQuery()
                        .eq(UserWechatInfoPO::getUserId, angelUserId.getId())
        );

        UserLevelPO userLevelPO = angelUserLevelMapper.selectOne(
                Wrappers.<UserLevelPO>lambdaQuery()
                        .eq(UserLevelPO::getUserId, angelUserId.getId())
        );

        AngelLevelPO angelLevelPO = angelLevelMapper.selectById(userLevelPO.getAngelLevelId());

        AngelUser angelUser = AngelUserFactory.create(
                angelUserId,
                new AngelUserInfo(
                        angelUserId.getId(),
                        WeChatInfo.create(
                                new WeChatNickName(wechatInfoPO.getNickName()),
                                new WeChatOpenId(wechatInfoPO.getOpenId()),
                                new WeChatAvatarUrl(wechatInfoPO.getAvatarUrl())
                        ),
                        new UserBasicInfo(userInfoPO.getNickName(),new HeadImage(userInfoPO.getHeadImage()))),
                new AngelUserLevelInfo(
                        AngelLevel.toAngelLevel(
                                angelLevelPO.getLevelId(),
                                angelLevelPO.getLevelName(),
                                angelLevelPO.getExperienceValue()
                        ),
                        userLevelPO.getNowExperienceValue()
                ),
                new CreateInfo(angelUserId,userInfoPO.getCreateTime())
        );

        return Optional.ofNullable(angelUser);
    }
}
